/**********************************************************************************************************************
| Model Name    : GPIO
| File Name     : GPIO.c
|
| Description   : //Description
|                       
|                 
|                               
|----------------------------------------------------------------------------------------------------------------------
|                                   C O P Y R I G H T
|----------------------------------------------------------------------------------------------------------------------
|   Copyright (c) by Zhejiang WanChao Electric .     All rights reserved.
|
|   This software is copyright protected and proprietary to Zhejiang WanChao Electric.
|
|   Zhejiang WanChao Electric grants to you only those rights as set out in the license conditions.  
|   
|   All other rights remain with Zhejiang WanChao Electric.
|
|----------------------------------------------------------------------------------------------------------------------
|                               A U T H O R   I D E N T I T Y
|----------------------------------------------------------------------------------------------------------------------
| Initials     Name                      Company
| --------     ---------------------     ------------------------------------------------------------------------------
| HAM           He Ai Min             Zhejiang WanChao Electric.
|----------------------------------------------------------------------------------------------------------------------
|               R E V I S I O N   H I S T O R Y
|----------------------------------------------------------------------------------------------------------------------
| Date       Version   Author  Description
|----------  --------  ------  ----------------------------------------------------------------------------------------
|2024-10-01  S1.0      HAM     - Creation
|*************************************************************************************************************************/


/*************************************************************************************************************************
Include File Segment
**************************************************************************************************************************/
#include "IF_GPIO.h"

/*************************************************************************************************************************
Const Define Segment
**************************************************************************************************************************/
/*    A        |     B         |      C     |       D     |     E     |     T        |           S            |       M          |       P                  |     J                  |     AD0       |      AD1
*------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
*    Data            Data           Data          Data         Data       Data                  Data                Data                 Data                      Data                  Data             Data
*                                                                        Input                 Input                Input               Input                     Input                  Input            Input
*   Direction      Direction      Direction    Direction    Direction   Direction              Direction           Direction            Direction                 Direction             Direction        Direction
*   PUCR-Pull Control Register                                          Pull Device           Pull Device          Pull Device          Pull Device               Pull Device           Pull Device      Pull Device
*                                                                       Polarity Select      Polarity Select      Polarity Select      Polarity Select            Polarity Select      Polarity Select   Polarity Select
*                                                                                            Wired-Or Mode        Wired-Or Mode
*                                                                                            Routing              Package Code
*                                                                                                                                     Interrupt Enable           Interrupt Enable     Interrupt Enable   Interrupt Enable
*                                                                                                                                     Interrupt Flag             Interrupt Flag       Interrupt Flag     Interrupt Flag
*/
/* const variable declaration */

static GPIO_Type  *GPIO_RegPort[] = {GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG, GPIOH};

/*************************************************************************************************************************
Function Declare Segment
**************************************************************************************************************************/


/*************************************************************************************************************************
 Global Variable Define Segment
**************************************************************************************************************************/

/*************************************************************************************************************************
| NAME:             GPIO_SetPinDirOut
| PROTOTYPE:
| CALLED BY:
| PRECONDITIONS:
| INPUT PARAMETERS: uint8 portNum,
uint8 pinNum
| RETURN VALUE:     Null
|
|
| DESCRIPTION:      GPIO Set Pin Dir Out
|
|*************************************************************************************************************************/
void IF_GPIO_SetPinDirOut(uint8 u8PortNum, uint8 u8PinNum)
{
	// #ifdef FM33FG0XXA
	if(u8PortNum > 7) return ;
	// #endif
	FL_GPIO_SetPinMode(GPIO_RegPort[u8PortNum], ((uint32)1 << u8PinNum), FL_GPIO_MODE_OUTPUT); 
}

void IF_GPIO_SetPinMode(uint8 u8PortNum, uint8 u8PinNum, uint8 mode)
{
	if(u8PortNum > 7) return ;
    /* Pin脚模拟模式设置 */
    if(mode == FL_GPIO_MODE_ANALOG)
    {
        FL_GPIO_DisablePinInput(GPIO_RegPort[u8PortNum], ((uint32)1 << u8PinNum));
        FL_GPIO_DisablePullUp(GPIO_RegPort[u8PortNum], ((uint32)1 << u8PinNum));
		FL_GPIO_DisablePullDown(GPIO_RegPort[u8PortNum], ((uint32)1 << u8PinNum));
        FL_GPIO_DisablePinOpenDrain(GPIO_RegPort[u8PortNum], ((uint32)1 << u8PinNum));  
        
    }
    else
    {
        /* Pin脚输入使能控制 */
        if(mode == FL_GPIO_MODE_INPUT)
        {
            FL_GPIO_EnablePinInput(GPIO_RegPort[u8PortNum], ((uint32)1 << u8PinNum));
        }
        else
        {
            FL_GPIO_DisablePinInput(GPIO_RegPort[u8PortNum], ((uint32)1 << u8PinNum));
			/* Pin脚驱动强度设置 */
            FL_GPIO_SetPinDriveStrength(GPIO_RegPort[u8PortNum], ((uint32)1 << u8PinNum), FL_GPIO_DRIVESTRENGTH_X3);			
		}
		/* Pin脚输出模式设置 */
        FL_GPIO_DisablePinOpenDrain(GPIO_RegPort[u8PortNum], ((uint32)1 << u8PinNum));//FL_GPIO_OUTPUT_PUSHPULL
	}
    /* Pin脚工作模式设置 */
    FL_GPIO_SetPinMode(GPIO_RegPort[u8PortNum], ((uint32)1 << u8PinNum), mode);	
}
/*************************************************************************************************************************
| NAME:              GPIO_SetPinDirIn
| PROTOTYPE:
| CALLED BY:
| PRECONDITIONS:
| INPUT PARAMETERS:  uint8 portNum,
uint8 pinNum
| RETURN VALUE:      Null
|
|
| DESCRIPTION:       GPIO Set Pin Dir In
|
|*************************************************************************************************************************/
void IF_GPIO_SetPinDirIn(uint8 u8PortNum, uint8 u8PinNum)
{
	FL_GPIO_SetPinMode(GPIO_RegPort[u8PortNum], ((uint32)1 << u8PinNum), FL_GPIO_MODE_INPUT); 
}

/*************************************************************************************************************************
| NAME:               GPIO_WritePinData
| PROTOTYPE:
| CALLED BY:
| PRECONDITIONS:
| INPUT PARAMETERS:   uint8 portNum,
uint8 pinNum,
uint8 pinLevel
| RETURN VALUE:       Null
|
|
| DESCRIPTION:       GPIO Write Pin Data
|
|*************************************************************************************************************************/
void IF_GPIO_WritePinData(uint8 u8PortNum, uint8 u8PinNum, uint8 u8PinLevel)
{
	if(u8PortNum > 7) return ;
	if (u8PinLevel)
	{
		GPIO_RegPort[u8PortNum] -> DSET |= (uint32)1 << u8PinNum;
		//*((volatile uint8 *)cGPIO_RegData[u8PortNum]) |= (uint8)1 << u8PinNum;
	}
	else
	{
		GPIO_RegPort[u8PortNum] -> DRST |= (uint32)1 << u8PinNum;
		//*((volatile uint8 *)cGPIO_RegData[u8PortNum]) &= ~((uint8)1 << u8PinNum);
	}
}

/*************************************************************************************************************************
| NAME:               GPIO_TogglePin
| PROTOTYPE:
| CALLED BY:
| PRECONDITIONS:
| INPUT PARAMETERS:   uint8 portNum,
uint8 pinNum
| RETURN VALUE:       Nul
|
|
| DESCRIPTION:        GPIO Toggle Pin
|
|*************************************************************************************************************************/
void IF_GPIO_TogglePin(uint8 u8PortNum, uint8 u8PinNum)
{
	FL_GPIO_ToggleOutputPin(GPIO_RegPort[u8PortNum], ((uint32)1 << u8PinNum));
}

/*************************************************************************************************************************
| NAME:              GPIO_ReadPinData
| PROTOTYPE:
| CALLED BY:
| PRECONDITIONS:
| INPUT PARAMETERS:  uint8 portNum,
uint8 pinNum
| RETURN VALUE:      pin state
|
|
| DESCRIPTION:       GPIO Read Pin Data
|
|*************************************************************************************************************************/
uint8 IF_GPIO_ReadPinData(uint8 u8PortNum, uint8 u8PinNum)
{
	uint8 Ret;
//	Ret = (*((volatile uint8 *)cGPIO_RegData[u8PortNum]) >> u8PinNum) & 0x01;
	Ret = ((GPIO_RegPort[u8PortNum] -> DIN & ((uint32)1 << u8PinNum)) != 0U) ? 1 : 0;
	return (Ret);
}

/*************************************************************************************************************************
| NAME:              GPIO_ReadPinInput
| PROTOTYPE:
| CALLED BY:
| PRECONDITIONS:
| INPUT PARAMETERS:  uint8 portNum,uint8 pinNum
| RETURN VALUE:      input state
|
|
| DESCRIPTION:       GPIO Read Pin Input
|
|*************************************************************************************************************************/
uint8 IF_GPIO_ReadPinInput(uint8 u8PortNum, uint8 u8PinNum)
{
	uint8 Ret;
	if (u8PortNum <= 8)
	{
//		Ret = (*((volatile uint8 *)cGPIO_RegInput[u8PortNum - GPIO_PORT_T]) >> u8PinNum) & 0x01;
		Ret = ((GPIO_RegPort[u8PortNum] -> DIN & ((uint32)1 << u8PinNum)) != 0U) ? 1 : 0;
	}
	else
	{
		/* do ntohing */
	}

	return (Ret);
}

/*************************************************************************************************************************
| NAME:              GPIO_SetPinPull
| PROTOTYPE:
| CALLED BY:
| PRECONDITIONS:
| INPUT PARAMETERS:  uint8 portNum,
uint8 pinNum,
uint8 pullMode
| RETURN VALUE:      Null
|
|
| DESCRIPTION:       GPIO Set Pin Pull
|
|*************************************************************************************************************************/
void IF_GPIO_SetPinPull(uint8 u8PortNum, uint8 u8PinNum, uint8 u8PullMode)
{
	if(u8PortNum > 7) return ;
	if (u8PullMode == GPIO_PULL_DISABLE)
	{
		FL_GPIO_DisablePullUp(GPIO_RegPort[u8PortNum], ((uint32)1 << u8PinNum));
		FL_GPIO_DisablePullDown(GPIO_RegPort[u8PortNum], ((uint32)1 << u8PinNum));
	}
	else if (u8PullMode == GPIO_PULL_MODE_UP)
	{
		FL_GPIO_EnablePullUp(GPIO_RegPort[u8PortNum], ((uint32)1 << u8PinNum));
		FL_GPIO_DisablePullDown(GPIO_RegPort[u8PortNum], ((uint32)1 << u8PinNum));
	}
	else if (u8PullMode == GPIO_PULL_MODE_DOWN)
	{
		FL_GPIO_DisablePullUp(GPIO_RegPort[u8PortNum], ((uint32)1 << u8PinNum));
		FL_GPIO_EnablePullDown(GPIO_RegPort[u8PortNum], ((uint32)1 << u8PinNum));
	}
	else
	{
		/* do nothing */
	}
}


